package com.anXin.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anXin.gpt.BigModelNew;
import com.anXin.gpt.ChatSparkDesk;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.user.dtos.OldmanInfoDto;
import com.anXin.model.user.dtos.UserInfoDto;
import com.anXin.model.user.pojos.Oldman;
import com.anXin.model.user.pojos.Report;
import com.anXin.model.user.pojos.User;
import com.anXin.user.mapper.OldmanMapper;
import com.anXin.user.mapper.ReportMapper;
import com.anXin.user.service.OldmanService;
import com.anXin.user.service.ReportService;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;


@Service("oldmanService")
public class OldmanServiceImpl extends ServiceImpl<OldmanMapper, Oldman> implements OldmanService {


    @Resource
    private ReportService reportService;
    @Override
    public ResponseResult getOldList() {
        User user = AppThreadLocalUtil.getUser();
        if (user==null)return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        List<Oldman> userOldmen = query().eq("user_id", user.getId()).list();
        if (userOldmen==null)return ResponseResult.errorResult(AppHttpCodeEnum.USER_OLDMAN_DATA_EXIST);
        return ResponseResult.okResult(userOldmen);
    }

    @Override
    public ResponseResult addOld(Oldman oldman) {
        oldman.setUserId(AppThreadLocalUtil.getUser().getId());
        boolean save = save(oldman);
        if (save)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult delete(Long id) {
        boolean removeById = removeById(id);
        if (removeById)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult updateOld(Oldman oldman) {
        boolean update = updateById(oldman);
        if (update)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult cookbook(Long id) throws Exception {
        ArrayList<Report> reports = (ArrayList<Report>) reportService.getList(id).getData();
        if (reports.isEmpty())return ResponseResult.errorResult(AppHttpCodeEnum.REPORT_LIST_NULL);
        Report report = reports.get(0);
        String answer = BigModelNew.Talk(report.getDescription() + "请你根据以上数据生成科学的膳食食谱");
        return ResponseResult.okResult(answer);
    }

    @Override
    public ResponseResult carePlan(Long id) throws Exception {
        ArrayList<Report> reports = (ArrayList<Report>) reportService.getList(id).getData();
        if (reports.isEmpty())return ResponseResult.errorResult(AppHttpCodeEnum.REPORT_LIST_NULL);
        Report report = reports.get(0);
        OldmanInfoDto oldmanInfoDto = (OldmanInfoDto) oldmanInfo(id).getData();
        String sex="";
        if (oldmanInfoDto.getSex()==0)sex+="男";
        else if (oldmanInfoDto.getSex()==1)sex+="女";
        else sex+="未知";
        String description="该老人性别为："+sex+"\n"+"该老人的爱好为:"+oldmanInfoDto.getHobby()+"\n"+"该老人的生日为："+oldmanInfoDto.getBirthday()+"\n"+"该老人的年龄为："+oldmanInfoDto.getAge()+"\n";
        String answer = BigModelNew.Talk(description+report.getDescription() + "请你根据以上数据生成针对该老人的照顾计划");
        return ResponseResult.okResult(answer);
    }

    @Override
    public ResponseResult oldmanInfo(Long id) {
        Oldman oldman = query().eq("id", id).one();
        if (ObjectUtil.isEmpty(oldman))return ResponseResult.errorResult(AppHttpCodeEnum.DATA_LIST_NULL);
        OldmanInfoDto oldmanInfoDto = BeanUtil.copyProperties(oldman, OldmanInfoDto.class);
        oldmanInfoDto.setAge(getAge(oldman.getBirthday()));
        return ResponseResult.okResult(oldmanInfoDto);
    }

    private Integer getAge(Date birthDay) {
        if (birthDay == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            return 0;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        boolean lowAge = monthBirth == monthNow && dayOfMonthNow < dayOfMonthBirth;

        if (monthNow < monthBirth || lowAge) {
            age--;
        }
        return age;
    }

}